define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'prom/cutdown/index',
                    add_url: 'prom/cutdown/add',
                    edit_url: 'prom/cutdown/edit',
                    del_url: 'prom/cutdown/del',
                    multi_url: 'prom/cutdown/multi',
                    table: 'cutdown',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'goods_id', title: __('Goods_id'),visible:false},
                        {field: 'goods_name', title: __('Goods_name')},
                        {field: 'price', title: __('Price'), operate:'BETWEEN'},
                        
                        {field: 'join_count', title: __('Join_count')},
                        {field: 'description', title: __('Description')},
                        {field: 'item_id', title: __('Item_id')},
                        {field: 'sales_sum', title: __('Sales_sum')},
                        {field: 'weigh', title: __('Weigh')},
                        {field: 'status', title: __('Status'), visible:false, searchList: {"0":__('Status 0'),"1":__('Status 1')}},
                        {field: 'status_text', title: __('Status'), operate:false},
                        {field: 'time_limit', title: __('Time_limit')},
                        {field: 'min_cut_price', title: __('Min_cut_price'), operate:'BETWEEN'},
                        {field: 'max_cut_price', title: __('Max_cut_price'), operate:'BETWEEN'},
                        {field: 'image', title: __('Image'), formatter: Table.api.formatter.image},
                        {field: 'start_time', title: __('Start_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'end_time', title: __('End_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'is_end_text', title: __('Is_end')},
                        {field: 'is_end', title: __('Is_end'), visible: false ,searchList: {0:__('Is_end 0'),1:__('Is_end 1')}},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                $(document).ready(function(){

                });

                $(document).on('change','#c-goods_id',function(){
                    var goods_id = $(this).val();
                    getAttrByGoodsId(goods_id);
                });

                //根据商品id获取规格
                function getAttrByGoodsId(goods_id){

                    $.ajax({
                        type: 'get',
                        url: '/admin/prom/cutdown/getAttrByGoodsId',
                        data: {goods_id: goods_id},
                        success: function(ret){
                            var html = "";
                            console.log(ret);
                            if(ret.data){
                                $.each(ret.data,function(k,v){
                                    html += "<option value='"+v.id+"'>"+v.attr_value+"</option>";
                                });

                            }
                            console.log(html);

                            $('#c-item_id').html(html);
                            
                        }

                    });
                }
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});